function  refre(){
    departmenttableI.reload('departmenttableId');
}
var departmenttableI = null;
layui.use(['table', 'laydate','form'], function(){
        var table = layui.table,
        laydate = layui.laydate,
        form = layui.form,
        data = null;
        laydate.render({
                elem: '#rq', //指定元素
        });

    departmenttableI = table.render({
                elem: '#test'
                ,url:'/department/getAll'
                ,toolbar:'#toolbarDemo'
                ,id:'departmenttableId'
                ,page:{
                        limit:10,
                        limits:[10,12,14],
                        layout:['count','prev','page','next','limit'],
                        groups:2
                    }
                ,cols: [
                    [
                          {type: 'checkbox', fixed: 'left'}
                        ,{field:'deptId', width:120, title: '序号', sort: true}
                        ,{field:'deptName', width:200, title: '部门名称'}
                        ,{field:'deptMs', width:130, title: '部门描述'}
                        ,{field:'admintorName', width:200, title: '创建人'}
                        ,{field:'deptState', title: '是否启用', width: 130,templet: function(row){
                                    var str = '<a lay-event="required">';
                                    if(row.deptState == '启用'){
                                        str += '<input type="checkbox" name="deptState" checked lay-skin="switch" lay-text="启用|不启用">';
                                    }else if(row.deptState == '不启用'){
                                        str += '<input type="checkbox" name="deptState" lay-skin="switch" lay-text="启用|不启用">';
                                    }
                                    str += '</a>';
                                    return str;
                                }
                        }
                         ,{field:'deptCreatdate', title: '创建时间',  width: 300}
                        ,{fixed: '', title:'操作', minWidth: 150,toolbar: '#barDemo'}
                ]
                ],
                skin: 'line',
                parseData:function (res){
                        return{
                                "code":res.code, // 解析接口状态
                                "msg": res.msg, // 解析提示文本
                                "count": res.count, // 解析总条数
                                "data": res.data // 解析数据列表
                        };
                }
        });

        table.on('tool(test)', function(obj){ // 双击 toolDouble
                data = obj.data;
                //console.log(obj)
                if(obj.event === 'del'){
                        layer.confirm('真的删除行么', function(index){
                                // layer.close(index);
                            $.ajax({
                                type:"post",
                                url:'/department/del',
                                data: {"deptId":data.deptId},
                                dataType:'json',
                                success:function (res) {
                                    if (res.start == 200){
                                        layer.msg(res.msg,{time:500,icon:6})
                                        refre();
                                    }
                                    else {
                                        layer.msg(res.msg,{time:500,icon:5})
                                    }
                                },
                                error:function(e){
                                    console.log("异常");

                                }

                            })

                        });
                } else if(obj.event === 'edit'){
                    layer.open({
                        type:2,
                        title:'编辑部门',
                        content:'departmentedit.html',
                        shade:[0.5,'#AFEEEE'],
                        anim:4,
                        area:['40%','65%'],
                        offset: ['150px', '300px'],
                        success:function (layero,index){
                            let item = localStorage.getItem("uname");
                            var bo = layer.getChildFrame('body',index);
                            bo.find('input[name="deptId"]').val(data.deptId);
                            bo.find('input[name="deptName"]').val(data.deptName);
                            bo.find('input[name="admintorName"]').val(item);
                            bo.find('textarea[name="deptMs"]').val(data.deptMs);
                        }
                    })
                }else if(obj.event === 'required'){
                    // alert("序号:"+data.deptId+"是否启用:"+data.deptState);
                       switch (data.deptState) {
                           case "启用":
                               qy("不启用");
                               break;
                           case "不启用":
                               qy("启用")
                               break;
                    }
                }

        });

        form.on('submit(go)',function (d){
                d = d.field;
                // console.log(d);
                table.reload('departmenttableId',{
                        where:{
                                deptName:d.deptName
                                ,admintorName:d.admintorName
                                ,deptCreatdate:d.deptCreatdate
                                ,deptState:d.deptState
                        }
                });
        })
    //监听顶部重置按钮
    form.on('submit(cz)',function (d){
        $("[name=deptState]").val('')
        $("[name=deptName]").val(undefined)
        $("[name=admintorName]").val(undefined)
        $("[name=deptCreatdate]").val(undefined)

        form.render()
        departmenttableI.reload({
            url:'/department/getAll',
        },true)
        return false
    })
    function qy(sta) {
        $.ajax({
            type:"post",
            url:'/department/updState',
            data: {"deptId":data.deptId,"deptState":sta},
            dataType:'json',
            success:function (res) {
                if (res.state == 200){
                    layer.msg(res.msg,{time:500,icon:6})
                }
                else {
                    layer.msg(res.msg,{time:500,icon:5})
                }
            },
            error:function(e){
                console.log("异常");

            }

        })
    }
    $("#tjbut").click(function () {
        layer.open({
            type:2,
            title:'添加部门',
            content:'departmentadd.html',
            shade:[0.5,'#AFEEEE'],
            anim:4,
            area:['700px','500px'],
            offset: ['200px', '350px'],
            success:function (layero,index){
                let item = localStorage.getItem("uname");
                var bo = layer.getChildFrame('body',index);
                bo.find('input[name="admintorName"]').val(item);
            }
        })
    })
});
